package io.demo.message.benchmark;

import static org.apache.commons.lang3.ClassUtils.getShortCanonicalName;

import com.google.common.collect.ImmutableList;
import io.demo.message.domain.Customer;
import io.demo.message.serial.SerializedObject;
import io.demo.message.util.TestingModels;
import java.util.List;
import java.util.function.Function;
import java.util.stream.IntStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Space consumption benchmarks
 *
 * @author 黎先生
 */
class SpaceBenchmarks {

  private static final Logger LOGGER = LoggerFactory.getLogger(SpaceBenchmarks.class);

  private final List<Function<Customer, SerializedObject<Customer>>> list;

  SpaceBenchmarks() {
    list = ImmutableList.of(
        SerializedObject::jdk,
        SerializedObject::protobuf,
        SerializedObject::kryo,
        SerializedObject::hessian2,
        SerializedObject::jackson,
        SerializedObject::gson,
        SerializedObject::fastjson
    );
  }

  void execute() {
    IntStream.range(0, 20).forEach(i -> {
      Customer customer = TestingModels.customer();
      list.forEach(action -> {
        SerializedObject<Customer> serializedObject = action.apply(customer);
        LOGGER.info("Customer:{}, Strategy:{}, Byte Length:{}",
            customer.getId(),
            getShortCanonicalName(serializedObject.getClass()),
            serializedObject.getByteArrayLength());
      });
    });
  }
}
